package com.panda.controller;

import com.panda.service.BinaryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.*;

/*

    一个账号最多可以申请三个不同的组名,账号用户名是一个默认的组名.所以最多可以有四个组名



 */

@RestController
public class Controller {

    private BinaryService binaryService;

    @Autowired
    public void setBinaryService(BinaryService binaryService) {
        this.binaryService = binaryService;
    }

    @PostMapping(value = "/push", consumes = MediaType.APPLICATION_OCTET_STREAM_VALUE, produces = MediaType.ALL_VALUE)
    void push(@RequestParam String group,@RequestParam String root,@RequestParam String version, @RequestBody byte[] body) {

        Assert.isTrue(!group.equals(""),"未指定组名");
        Assert.isTrue(!root.equals(""),"未指定项目根路径");
        Assert.isTrue(!version.equals(""),"未指定版本号");

        String pre = group + "/" + root + "/" + version;

        binaryService.save(pre, body);

    }


    @GetMapping(value = "/pull/{group}/{root}/{version}", produces = MediaType.APPLICATION_OCTET_STREAM_VALUE)
    byte[] pull(@PathVariable String group, @PathVariable String root, @PathVariable String version
    ) {

        String pre = group + "/" + root + "/" + version;


        return binaryService.get(pre);

    }


}
